extends Area2D

var body = null					# 触碰到的碰撞箱

@onready var timer = $Timer		# 计时器
@onready var util = Util.new()
@onready var selfState = $"../State"
var HP_UI = null

func _ready():
	self.connect("area_entered", enemyHit)
	self.connect("area_exited", bodyExited)
	timer.connect("timeout", timeout)


'''
	玩家进入敌人攻击范围
	@param area 玩家
'''
func enemyHit(area):
	var body = area.get_parent()
	HP_UI = util.findOne("$[HP_UI]", util.findOne("#[CanvasLayer]", body))
	# 敌人进入攻击范围, 开始计时
	timer.start()
	# 获取到被攻击对象的state
	var bodyState = util.findOne("$[State]", body)
	# 第一次瞬间造成伤害
	# 之后如果一直在攻击范围, 每秒造成伤害
	bodyState.subX(selfState.power)
	
	self.body = body


'''
	玩家出攻击范围, 停止计时
'''
func bodyExited(_area):
	timer.stop()


'''
	每秒执行一次, 计时器
'''
func timeout():
	# 玩家的State子节点
	var bodyState = util.findOne("$[State]", body)
	bodyState.subX(selfState.power)
